/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	 Copyright (C) 2011
 *   @author prebz
 */

package com.wirelesstransfer.activity.send;

import java.io.File;

import com.wirelesstransfer.Main;
import com.wirelesstransfer.R;
import com.wirelesstransfer.activity.dialogs.Question;
import com.wirelesstransfer.activity.filebrowser.FileBrowser;
import com.wirelesstransfer.client.ApacheClient;
import com.wirelesstransfer.server.WIFI.JettyServer;
import com.wirelesstransfer.server.WIFI.handlers.Files;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

public class Send extends FileBrowser implements OnClickListener{

	private JettyServer server;

	private Files allFiles;
	
	private Resources res;

	// Dialog constants
	public static final int DIALOG_CONFIRM = 0;
	public static final int DIALOG_CANCEL = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send);	

		res = getResources();

		// Added files
		allFiles = new Files();
		
		// Click listeners for buttons
		View confirmButton = findViewById(R.id.confirm_button);
		confirmButton.setOnClickListener(this);
		View cancelButton = findViewById(R.id.cancel_button);
		cancelButton.setOnClickListener(this);

	}
	
	@Override
	public void onResume(){
		super.onResume();
				
		server = JettyServer.getInstance();
		
		Log.v("status", "Server isRunning()"  + server.isRunning());
		if(server.isRunning() || 
			ApacheClient.getInstance().isActiveClient()){
			
			buildInitDialog(res.getString(R.string.send_dialog_init).toString()).show();
			
		}
	}

	public void onClick(View v) {
		switch(v.getId()){
		case R.id.confirm_button:
			if(allFiles.getFiles().size() <= 0){
				buildOKDialog(res.getString(R.string.send_dialog_no_files)).show();
			}
			else {
				showDialog(DIALOG_CONFIRM);							
			}
			break;
		case R.id.cancel_button:
			showDialog(DIALOG_CANCEL);
			break;
		}
	}

	@Override
	protected Dialog onCreateDialog (int id){
		Dialog dialog = null;

		switch (id) {
		case DIALOG_CONFIRM:
			dialog = buildConfirmDialog(res.getString(R.string.send_dialog_confirm));
			break;
		case DIALOG_CANCEL:
			dialog = buildCancelDialog(res.getString(R.string.send_dialog_cancel));
			break;
		default:
			dialog = null;
			break;
		}
		return dialog;

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.questionmenu, menu);
		return true;
	}
	
	/**
	 * Switch to a specific tab
	 * @param indexTabToSwitchTo
	 */
	private void switchTabInActivity(int indexTabToSwitchTo){
        Main ParentActivity;
        ParentActivity = (Main) this.getParent();
        ParentActivity.switchTab(indexTabToSwitchTo);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.question_menu:
			Intent question = new Intent(this, Question.class);
			question.putExtra(res.getString(R.string.question_TITLE),res.getString(R.string.question_send_title_txt));
			question.putExtra(res.getString(R.string.question_TEXT),res.getString(R.string.question_send_txt));
			startActivity(question);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * Creates text in a dialog with yes/no key
	 * @param action
	 * @return
	 */
	public AlertDialog buildConfirmDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		.setCancelable(false)
		.setPositiveButton(res.getString(R.string.send_dialog_yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				
				try {
					server.startServer(allFiles);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
				switchTabInActivity(0);       
			}
		})
		.setNegativeButton(res.getString(R.string.send_dialog_no), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}

	/**
	 * Creates text in a dialog with yes/no key
	 * @param action
	 * @return
	 */
	public AlertDialog buildCancelDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		.setCancelable(false)
		.setPositiveButton(res.getString(R.string.send_dialog_yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				switchTabInActivity(0); 
			}
		})
		.setNegativeButton(res.getString(R.string.send_dialog_no), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}
	
	/**
	 * Creates text in a dialog with yes/no key
	 * @param action
	 * @return
	 */
	public AlertDialog buildInitDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		.setCancelable(false)
		.setPositiveButton(res.getString(R.string.send_dialog_yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				server.stopServer();
			}
		})
		.setNegativeButton(res.getString(R.string.send_dialog_no), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				switchTabInActivity(0); 	
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}
	
	/**
	 * Creates text in a dialog with Ok key
	 * @param action
	 * @return
	 */
	private AlertDialog buildOKDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		.setCancelable(false)

		.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		File file = new File(path.get(position));
		String fileName = file.getName();

		if (file.isDirectory()){
			if(file.canRead())
				getDir(path.get(position));
			else
				displayToast(fileName + res.getString(R.string.filebrowser_CANT_READ));

		}
		else{
			// File event
			// Adds and removes files
			if(allFiles.isInList(file)){
				allFiles.removeFile(file);
				displayToast(res.getString(R.string.filebrowser_removed_file)+ " " + fileName);
			}
			else{
				allFiles.addFile(file);
				displayToast(res.getString(R.string.filebrowser_add_file) + " " + fileName);
				
			}

		}
		
	}
	
}
